﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using Sim;
using Lists;
using Users;

namespace SimTest
{
    [TestFixture]
    public class TestClass2
    {
        [Test]
        public void TournamentSimulationTest()
        {
            DDList dd = new DDList();
            dd.Readfromfile();
            DDList.SerializeToXML();
            TournamentList.SerializeToXML(); //Skapar ny XML fil för turneringar
            UserList.SerializeToXML();      //Skapar ny användarlista

            Tournament t = new Tournament("VM2020", DateTime.Today, 10, "Male", 5, 3);
            TournamentList.AddTournament(t);

            //Registrering av användare =Simhoppare=
            UserList.AddUser(new User("Van", "Nguyen", 21, "VN", "Male", 0, "vanngh121", "hejhopp123"));
            UserList.AddUser(new User("Amjad", "Hematkar", 24, "IR", "Male", 0, "amjmah121", "lolol"));
            UserList.AddUser(new User("Gabriel", "Kasto", 23, "SY", "Male", 0, "gabkah121", "yoyo"));
            UserList.AddUser(new User("Adam", "Ludenberg", 22, "SE", "Female", 0, "adaluh121", "derp"));
            //Registrering av användare =Dommare=
            UserList.AddUser(new User("Dum1", "Dummare1", 12, "FI", "Male", 1, "j1", "jjj"));
            UserList.AddUser(new User("Dum2", "Dummare2", 12, "UK", "Male", 1, "j2", "iii"));
            UserList.AddUser(new User("Dum3", "Dummare3", 12, "US", "Male", 1, "j3", "hhh"));
            UserList.AddUser(new User("Dum4", "Dummare4", 12, "CN", "Male", 1, "j4", "ggg"));
            UserList.AddUser(new User("Dum5", "Dummare5", 12, "AU", "Male", 1, "j5", "fff"));
            UserList.AddUser(new User("Dum6", "Dummare6", 12, "DK", "Male", 1, "j6", "eee"));
            UserList.AddUser(new User("Dum7", "Dummare7", 12, "NO", "Male", 1, "j7", "ddd"));

            //Lägger till domare i tävlingen
            t.listOfJudges.AddJudge("j1");
            t.listOfJudges.AddJudge("j2");
            t.listOfJudges.AddJudge("j4");
            t.listOfJudges.AddJudge("j6");
            t.listOfJudges.AddJudge("j7");

            //Lägger till hoppare i tävlingen
            t.listOfDivers.AddDiver("vanngh121");
            t.listOfDivers.AddDiver("adaluh121");
            t.listOfDivers.AddDiver("gabkah121");

            //==HOPP1==
            //Hoppare 1 hoppar
            t.listOfDivers.diverList[0].dives.Add(new Dive());
            //Domarbedömning
            t.listOfDivers.diverList[0].dives[0].setJudgeScore(5.5, 3);
            t.listOfDivers.diverList[0].dives[0].setJudgeScore(3.5, 4);
            t.listOfDivers.diverList[0].dives[0].setJudgeScore(2, 5);
            t.listOfDivers.diverList[0].dives[0].setJudgeScore(6, 6);
            t.listOfDivers.diverList[0].dives[0].setJudgeScore(4.5, 7);
            //Kalkylering
            t.listOfDivers.diverList[0].dives[0].CalcPtsnDD("6265C", t.jumpHeight);

            //Hoppare 2 hoppar
            t.listOfDivers.diverList[1].dives.Add(new Dive());
            //Domarbedömning
            t.listOfDivers.diverList[1].dives[0].setJudgeScore(7, 3);
            t.listOfDivers.diverList[1].dives[0].setJudgeScore(3.5, 4);
            t.listOfDivers.diverList[1].dives[0].setJudgeScore(5, 5);
            t.listOfDivers.diverList[1].dives[0].setJudgeScore(6, 6);
            t.listOfDivers.diverList[1].dives[0].setJudgeScore(4.5, 7);
            //Kalkylering
            t.listOfDivers.diverList[1].dives[0].CalcPtsnDD("6261B", t.jumpHeight);

            //==HOPP2==
            //Hoppare 1 hoppar
            t.listOfDivers.diverList[0].dives.Add(new Dive());
            //Domarbedömning
            t.listOfDivers.diverList[0].dives[1].setJudgeScore(1, 3);
            t.listOfDivers.diverList[0].dives[1].setJudgeScore(6, 4);
            t.listOfDivers.diverList[0].dives[1].setJudgeScore(2, 5);
            t.listOfDivers.diverList[0].dives[1].setJudgeScore(6.5, 6);
            t.listOfDivers.diverList[0].dives[1].setJudgeScore(2.5, 7);
            //Kalkylering
            t.listOfDivers.diverList[0].dives[1].CalcPtsnDD("5233D", t.jumpHeight);

            //Hoppare 2 hoppar
            t.listOfDivers.diverList[1].dives.Add(new Dive());
            //Domarbedömning
            t.listOfDivers.diverList[1].dives[1].setJudgeScore(9.5, 3);
            t.listOfDivers.diverList[1].dives[1].setJudgeScore(10, 4);
            t.listOfDivers.diverList[1].dives[1].setJudgeScore(5, 5);
            t.listOfDivers.diverList[1].dives[1].setJudgeScore(6, 6);
            t.listOfDivers.diverList[1].dives[1].setJudgeScore(4.5, 7);
            //Kalkylering
            t.listOfDivers.diverList[1].dives[1].CalcPtsnDD("5311A", t.jumpHeight);

            TournamentList.SerializeToXML(); //Uppdaterar XML filen



            //============TEST=================
            Assert.AreEqual(11, UserList.userList.Count);
            //Loop för antal judges och divers i userlist 
            int judgeCounter = 0;
            int diverCounter = 0;
            foreach (var d in UserList.userList)
            {
                if (d.level == 0)
                    diverCounter++;
                else if (d.level == 1)
                    judgeCounter++;
            }

            Assert.AreEqual(4, diverCounter);
            Assert.AreEqual(7, judgeCounter);

            Assert.AreEqual(3, t.listOfDivers.diverList.Count); //Antal tävlande i tävlingen
            Assert.AreEqual(5, t.listOfJudges.judgeList.Count); //Antal domare i tävlingen

            Assert.AreEqual(2, t.listOfDivers.diverList[0].dives.Count); // Antal hopp som hoppare 1 har gjort.
            Assert.AreEqual(4, t.listOfDivers.diverList[0].dives.Count + t.listOfDivers.diverList[1].dives.Count); //totala hopp i tävlingen

            Assert.AreEqual(5, t.listOfDivers.diverList[0].dives[0].diveScore.Count); //Antal bedömningar hopp1 av hoppare 1
            Assert.AreEqual(20, t.listOfDivers.diverList[0].dives[0].diveScore.Count + t.listOfDivers.diverList[0].dives[1].diveScore.Count
                + t.listOfDivers.diverList[1].dives[0].diveScore.Count + t.listOfDivers.diverList[1].dives[1].diveScore.Count); //Totala bedömningar under tävlingen

            Assert.AreEqual(4.40, t.listOfDivers.diverList[0].dives[0].diveDifficulty);
            Assert.AreEqual(3.40, t.listOfDivers.diverList[1].dives[0].diveDifficulty);
            Assert.AreEqual(2.40, t.listOfDivers.diverList[0].dives[1].diveDifficulty);
            Assert.AreEqual(2.10, t.listOfDivers.diverList[1].dives[1].diveDifficulty);

            Assert.AreEqual((4.40 * 13.5), t.listOfDivers.diverList[0].dives[0].calculatedScore);
            Assert.AreEqual((3.40 * 15.5), t.listOfDivers.diverList[1].dives[0].calculatedScore);
            Assert.AreEqual((2.40 * 10.5), t.listOfDivers.diverList[0].dives[1].calculatedScore);
            Assert.AreEqual((2.10 * 20.5), t.listOfDivers.diverList[1].dives[1].calculatedScore);



            //UserList.userList.Clear();
            UserList.SerializeToXML();

            //TournamentList.tournamentList.Clear();
            TournamentList.SerializeToXML();


        }
        
    }
}

